var modelgroupStore=new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showCompanyModelgroupList',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : 'ModelgroupVO',
		totalProperty : 'totalModelgroupVO',
		id : 'id'
	}, [{
		name : 'id'
	}, {
		name : 'groupName'
	}, {
		name : 'description'
	}, {
		name : 'status'
	}]),
	remoteSort: false
});

//表格列模型
var modelgroupsm=new Ext.grid.CheckboxSelectionModel ({singleSelect : true});
var modelgroupColumnModel=new Ext.grid.ColumnModel([privilegesm,
	new Ext.grid.RowNumberer(),
	{
		header : '模型组编号',
		width : 100,
		dataIndex : 'id',
		sortable : true,
		id : 'name'
	}, {
		header : '名称',
		width : 120,
		dataIndex : 'groupName',
		sortable : true,
		align : 'center'
	}, {
		header : '说明',
		width : 60,
		dataIndex : 'description',
		sortable : true,
		align : 'right'
	}, {
		header : '状态',
		width : 60,
		dataIndex : 'status',
		sortable : true,
		align : 'right',
		renderer:function(value, cellmeta, record){
			var typemap=[['0','未订购']
					,['1','待审批']
					,['2','已订购']];
			return typemap[value][1];
		}
	},
	{
		header : '操作',
		width : 120,
		dataIndex : 'status',
		sortable : false,
		renderer : function(value, cellmeta, record, rowIndex, columnIndex,store) {		
			var v=record.get('status');
			var recid=record.get('id');
			var btn="";
			if(0==v){
				btn='<div id="div_'+recid+'"><button type="button" onclick="updateModelStatus('+recid+',1)">订购</button></div>';
			}else{
				btn='<div id="div_'+recid+'"></div>';
			}
//			else if(1==v){
//				btn='<div id="div_'+recid+'"><button type="button" onclick="updateModelStatus('+recid+',0)">撤销订购</button></div>';
//			}else if(2==v){
//				btn='<div id="div_'+recid+'"><button type="button" onclick="updateModelStatus('+recid+',0)">收回模型</button></div>';
//			}
			return btn;
		}
	}]
);

var modelgroupList=new Ext.grid.EditorGridPanel({
	id:'modelgrouplist-panel',
	title:'订购模型',
	showPage:function(){
		modelgroupStore.load();
	},
	enableColumnResize : true,
	border : true,
	clicksToEdit:1,
	enableColumnMove : true,
	enableHdMenu : false,
	stripeRows: true, //显示斑马线
	loadMask: true,
	autoHeight:true,
	bodyStyle:'width:100%',
    autoWidth:true,
    editable:true,
    sm : modelgroupsm,
	bbar: new Ext.PagingToolbar({//下面的分页条
        pageSize: 15,
        store: privilegeStore,
        displayInfo: true,
        displayMsg: '当前显示第 {0} 条到 {1} 条记录，一共 {2} 条',
        emptyMsg: "没有记录",
        lastText:"尾页",   
        nextText :"下一页",   
        prevText :"上一页",   
        firstText :"首页",   
        refreshText:"刷新页面"        
    }),
    tbar:[{
		xtype:'button',
		iconCls : 'db-icn-upload_',
		width:100,
		text:'提交修改',
		handler:submitModifiedPrivilege
	},
		{xtype : "tbseparator"}
	],
	cm : modelgroupColumnModel,
	store : modelgroupStore
	
});

function updateModelStatus(recid,status){
	Ext.Ajax.request({
		url: 'updateGroupCompany.action?method=updateGroupCompany&id='+recid
			+'&status='+status,				   	
	   	success: function(res,opt){	
	   		for(var i=0;i<modelgroupStore.totalLength;i++){
	   			var rec=modelgroupStore.getAt(i);
	   			if(rec.get('id')==recid){
	   				rec.set('status',status);
	   				var div=document.getElementById('div_'+recid);
	   				var btn="";
	   				if(0==status){
	   					btn='<button type="button" onclick="updateGroupCompany('+recid+',2)">授予模型</button>';
	   				}else if(1==status){
	   					btn='';
	   				}else if(2==status){
	   					btn='<button type="button" onclick="updateGroupCompany('+recid+',0)">收回模型</button>';
	   				}
	   				div.innerHTML=btn;
	   				break;
	   			}
	   		}
	   		Ext.Msg.alert('消息','操作成功！');
	   		
	   	},
	   	failure: function(){
	   		
	   	}
	});
}